import { getReferenceEntryRecord } from '@/api/parameter'

export default {
  data() {
    return {
      referenceValues: [],
      totalReferences: 1,
      referencePage: 1
    }
  },
  methods: {
    fetchReferenceData(visible, triggerByDropDown, item, type) {
      if (type === 'conditions') {
        this.currentType = 'currentCondition'
      } else if (type === 'operations') {
        this.currentType = 'currentOperation'
      }

      if (visible) {
        const arr = type === 'conditions' ? this._conditions : this.operations
        const current = arr.find((_) => _.id === item.id)
        const table = current.parameter.table
        if (triggerByDropDown) {
          this.valueSelectorLoading = true
          this.referenceValues.splice(0, this.referenceValues.length)
          this.totalReferences = 1
          this.referencePage = 1
        }
        if (this.totalReferences <= this.referenceValues.length) {
          return
        }

        getReferenceEntryRecord(this.templateId, table.id, {
          pageNo: this.referencePage,
          pageSize: 10
        }).then((data) => {
          this.valueSelectorLoading = false
          this.totalReferences = data.total
          this.referenceValues.push(...data.items)
        }).catch(err => {
          this.valueSelectorLoading = false
          this.$confirmError(err)
        })
      }
    },
    handleLoadMoreReference(item, type) {
      this.referencePage++
      this.fetchReferenceData(true, false, item, type)
    }
  }
}
